package com.jxj.daoImpl;




import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import org.springframework.stereotype.Repository;

import com.jxj.entity.Admin;
import com.jxj.entity.Demand;
import com.jxj.entity.User;

@Repository
public class UserDaoImpl{
	@Resource
	private SessionFactory sessionFactory;
	public Session getSession()
    {
        return sessionFactory.getCurrentSession();
    }
	//注册
	public void register(User user){		
		user.setName(user.getName());
		user.setPassword(user.getPassword());
		user.setSchool(user.getSchool());
		user.setSex(user.getSex());
		user.setTelephone(user.getTelephone());
		user.setXuehao(user.getXuehao());
		user.setMoney(0);
		getSession().save(user);				
	}
	//判断是否存在学号
	public boolean findUser(String xuehao){
		String hql="from User where xuehao=?";
		User user=null;
		try {
		Query query=getSession().createQuery(hql);
		query.setParameter(0, xuehao);
		user=(User)query.uniqueResult();
		System.out.println(user+"11");
		if(user == null){
			return true;
		}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return false;
					
	}
	//用户登录
	public boolean login(String xuehao,String password){
		String hql="from User where xuehao=? and password=?";	
		Query query=getSession().createQuery(hql);
		query.setParameter(0, xuehao);
		query.setParameter(1, password);
		User user=(User)query.uniqueResult();
		if (user != null) {
			return true;
		}else {
			return false;
		}
		
	}
	//管理员登录	
	public boolean loginAdmin(String xuehao,String password){
		String hql="from Admin where xuehao=? and password=?";
		Query query=getSession().createQuery(hql);
		query.setParameter(0, xuehao);
		query.setParameter(1, password);
		Admin admin=(Admin)query.uniqueResult();
		if (admin != null) {
			return true;
		}else {
			return false;
		}
	}
	//查找用户信息 显示在提交文案上
	public User findUser1(String xuehao){
		String hql="from User where xuehao=?";
		Query query=getSession().createQuery(hql);
		query.setParameter(0, xuehao);
		User user=(User)query.uniqueResult();		
		return user;
		
	}
	public void updateUser(String name,int telephone,String email,User user,String school){	
		user=getSession().load(User.class, user.getId());
		user.setSchool(school);
		user.setName(name);
		user.setTelephone(telephone);
		user.setEmail(email);	
		getSession().saveOrUpdate(user);		
	}
	
	public void updateMoney(double money,String xuehao){
		String hql="select money from User where xuehao=?";
		this.getSession().createQuery(hql).setParameter(0, xuehao).uniqueResult();
		String hql1="update User set money=money+? where xuehao=?";
		this.getSession().createQuery(hql1).setParameter(0, money).setParameter(1, xuehao).executeUpdate();
	    
	}
	
}
